/**
 * 工作任务视频关联
 */
Ext.define("Module.video.WorkVideoMatch",{
	extend : 'Ext.panel.Panel',
	layout: 'border',
	border: false,
	initComponent : function() {
		var me = this;
		var store = Ext.create('Ext.data.Store',{
			autoLoad: true,
			fields:['id','content','starttime','endtime','bzname'],
			proxy:{
				type:'ajax',
				extraParams:{task:'worksy'},
				url:'vm/getTaskList.do',
				reader:{
					root: 'datas',
					totalProperty: 'total'
				}
			}
    	});
		var workTask = Ext.create("Ext.grid.Panel",{
			id:me.id+'-workGrid',
			region: 'west',
			width: 740,
			store:store,
			tbar:[{
		    	labelWidth:60,
		    	fieldLabel:'任务内容',
		    	xtype: 'textfield',
		    	x:3,
		    	y:3
		    },{
		    	name:'startTime',
		    	xtype:'datefield',
		    	fieldLabel:'开始时间',
		    	labelWidth:60,
		    	format:'Y-m-d H:i:s',
		    	value:new Date(),
		    	x:220,
		    	y:3
		    },{
		    	name:'endTime',
		    	xtype:'datefield',
		    	fieldLabel:'结束时间',
		    	labelWidth:60,
		    	format:'Y-m-d H:i:s',
		    	value:new Date(),
	    		x:460,
		    	y:3	
		    },{
		    	x:670,
		    	y:3,
		    	xtype:'button',
		    	padding:'0 0 0 5',
		    	iconCls: 'icon-search',
		    	text: '查询',
		    	handler: function(){
	    			var content = this.up('grid').down('field[fieldLabel=任务内容]').getValue();
	    			var startTime = this.up('grid').down('field[name=startTime]').getSubmitValue();
	    			var endTime = this.up('grid').down('field[name=endTime]').getSubmitValue();
	    			if(new Date(startTime) > new Date(endTime)){
	    				Ext.Msg.alert("提示","开始时间不能大于结束时间");
	    				return;
	    			}
	    			
	    			store.proxy.extraParams = {
						task:'worksy',
						content:content,
						startTime:startTime,
						endTime:endTime
	    			};
					store.loadPage(1);
					Ext.getCmp(me.id+'vgrid').store.removeAll();
		    	}
		    }],
			bbar: Ext.create('Ext.PagingToolbar', {
	            store: store,
	            displayInfo: true,
	            displayMsg: '显示第 {0} 至 {1} 条记录，共 {2} 条',
	            emptyMsg: "无记录"
	        }),
        	columns:[
        	    {dataIndex:'content',width:260,text:'任务内容'},
        	    {dataIndex:'bzname',width:120,text:'班组'},
        		{dataIndex:'starttime',width:150,text:'开始时间',renderer:formatDate},
        		{dataIndex:'endtime',width:150,text:'结束时间',renderer:formatDate}
        	],
        	listeners:{
        		cellclick:function(s,td,index,rec){
        			Ext.getCmp(me.id+'vgrid').store.load({
        				params:{
        					taskId: rec.raw.id,
        					bzId:rec.raw.bzid
//        					status:1,
//        					taskType:2//1-表示巡视任务
        				}
        			});
        		},
		    	afterrender:function(){
		    		workTask.down('field[fieldLabel=任务内容]').setValue();
		    		workTask.down('field[name=startTime]').setValue();
		    		workTask.down('field[name=endTime]').setValue();
		    	}
		    }
		});
		
		var _fields = ['id','name','starttime','endtime','videotime','syntime','rawpath','synpath','video_id',
		               'bz_name','jsname','gcname','status','jsid','gcid'];
		var video = Ext.create("Ext.grid.Panel",{
			id:me.id+'vgrid',
			region: 'center',
			title:'关联视频',
			tbar:[{
				text:'添加视频',
				iconCls:'icon-add',
				handler:function(){
//					var rec = this.up('tabpanel').down('grid').getSelectionModel().getSelection()[0];
					var rec =  Ext.getCmp(me.id+'-workGrid').getSelectionModel().getSelection()[0];
//					var taskType = Ext.getCmp(me.id+'taskTab').getActiveTab().taskType;
					if(rec == undefined){
						Ext.Msg.alert('提示','请选择任务。');
						return;
					}
					me.addwin(rec,1);
				}
			},{
				text:'修改视频',
				iconCls:'icon-edit',
				handler:function(){
					var rec = Ext.getCmp(me.id+'-workGrid').getSelectionModel().getSelection()[0];
					if(rec == undefined){
						Ext.Msg.alert('提示','请选择任务');
						return;	
					}
					var vmrec = Ext.getCmp(me.id+'vgrid').getSelectionModel().getSelection()[0];
					if(vmrec == undefined){
						Ext.Msg.alert('提示','请选择已关联的视频。');
						return;
					}
//					me.twin(d,true);
					me.upwin(rec, 1, vmrec);
				}
			},{
				text:'删除视频',
				iconCls:'icon-delete',
				handler:function(){
					var rec = video.getSelectionModel().getSelection()[0];
					if(!rec){
						Ext.Msg.alert('提示','请选择已关联的视频。');
						return;
					}
					me.delwin(rec,video);
				}
			},{
				text:'播放',
				iconCls:'icon-play',
				handler:function(){
					var rec = Ext.getCmp(me.id+'vgrid').getSelectionModel().getSelection()[0];
					if(rec == undefined){
						Ext.Msg.alert("提示","请选择要播放的视频。");
						return ;
					}
					if(!rec.raw.video_id){
						Ext.Msg.alert("提示","此作业过程无视频，请重新选择。");
						return ;
					}
					me.playWin({id:rec.raw.video_id,synpath: basePath+rec.raw.synpath});
				}
			}],
			columns:[{
				dataIndex:'jsname',
				width:100,
				text:'角色'
			},{
				dataIndex:'gcname',
				width:100,
				text:'作业过程'
			},{
				dataIndex:'name',
				width:200,
				text:'视频名称',
				renderer: function(v){
					return (v == null || v == '')?'<font color="blue">无视频</font>':v;
				}
			}/*,{
				dataIndex:'syntime',
				width:130,
				text:'同步时间',
				renderer: formatDate
			}*/,{
				dataIndex:'videotime',
				width:80,
				text:'视频时长（分）',
				renderer:function(v){
					if(v == null || v == ''){
						return 0;
					}
					var t = v/1000;
				    var m = Math.floor(t / 60);
				    m < 10 && (m = '0' + m);
				    return m + ":" + (t % 60 / 100).toFixed(2).slice(-2);
				}
			}/*,{
				dataIndex:'rawpath',
				width:200,
				text:'原始路径'
			},{
				dataIndex:'synpath',
				width:200,
				text:'同步路径'
			}*/],
			store:Ext.create('Ext.data.Store',{
//				autoLoad: true,
				fields:_fields,
				proxy:{
					type:'ajax',
//					extraParams:{status:1},
					url:'vm/getJCandgcVideo.do'
				}
			}),
			listeners:{
				cellclick:function(c,td,cellIndex,rec){
					if(cellIndex == 2){
						if($(td).find('font').html() == '无视频'){
							var rec = Ext.getCmp(me.id+'-workGrid').getSelectionModel().getSelection()[0];
							var vmrec = Ext.getCmp(me.id+'vgrid').getSelectionModel().getSelection()[0];
							me.upwin(rec, 1, vmrec);
						}
					}
				}
			}
		});
		
		me.items = [workTask,video];
		me.callParent();
	},
	addwin:function(rec,taskType){
		var me = this;
		var _awin = Ext.create("Ext.window.Window",{
			layout:'border',
			width:790,
			height:500,
			modal:true,
			iconCls:'icon-add',
			title:'添加任务视频',
			buttons:[{
				text:'保存',
				handler:function(){
					var thiswin = this.up("window");
					var taskId = rec.raw.id;
					//var bzId = thiswin.down('combobox[name=bzname]').getValue();
					var roleId = thiswin.down('combobox[name=bzjs]').getValue();
					var stageId = thiswin.down('combobox[name=gzjd]').getValue();
					var videoId = thiswin.down('grid').getSelectionModel().getSelection()[0];
//					if(bzId == undefined){
//						Ext.Msg.alert('提示',"请选择工作班组");return;
//					}
					if(roleId == undefined){
						Ext.Msg.alert('提示',"请选择班组角色");return;
					}
					if(stageId == undefined){
						Ext.Msg.alert('提示',"请选工作阶段");return;
					}
					if(videoId == undefined){
						Ext.Msg.alert('提示',"请选择要关联的视频");return;
					}
					Ext.Ajax.request({
						url:'vm/addVideoManage.do',
						params:{
//							bz_id:bzId,
							bz_id:rec.raw.bzid,
							role_id:roleId,
							stage_id:stageId,
							video_id:videoId.raw.id,
							task_id:taskId,
							taskType:taskType
						},
						success:function(resp){
							if(resp.responseText == 1){
								Ext.Msg.alert("提示","关联视频成功");
								//var tabGrid = this.up('tabpanel').down('grid').getSelectionModel().getSelection()[0];
//								thiswin.down('grid').store.reload();
								Ext.getCmp(me.id+'vgrid').store.reload();
								_awin.close();
							}else if(resp.responseText == 11){
								Ext.Msg.alert("提示","此作业过程已存在关联视频。请重新选择");
							}
							else if(resp.responseText == 10){
								Ext.Msg.confirm('提示', '此视频已关联，点击"确定"将覆盖原数据？',function(x){
									if(x == 'yes'){
										Ext.Ajax.request({
											url:'vm/updateVm.do',
											params:{
//												bz_id:bzId,
												bz_id:rec.raw.bzid,
												video_id:videoId.raw.id,
												stage_id:stageId,
												task_id:taskId,
												role_id:roleId,
												mark:'update',//判断是否为修改数据标识
												delId:videoId.raw.id //已经关联的视频id,传入后天删除相关的数据
											},
											success:function(resp){
												//var d =Ext.decode(resp.responseText);
												if(resp.responseText == 1){
													Ext.getCmp(me.id+'vgrid').store.reload();
													_awin.close();
													Ext.Msg.alert('提示', '关联成功！');
												}else{
													Ext.Msg.alert('提示', '关联失败！');
												}
											}
										});
									}
								});
							}
							else{
								Ext.Msg.alert("提示","关联视频失败");
							}
						}
					});
				}
			},{
				text:'取消',
				handler:function(){
					this.up("window").close();
				}
			}],
			items:[{
				region: 'north',
				xtype:'form',
				bodyPadding: 10,
				border: false,
				defaults: {anchor: '100%',allowBlank: false},
				items:[{
					xtype:'combobox',
					name:'bzjs',
					fieldLabel: '班组角色',
					readOlay:true,
					displayField: 'name',
					valueField: 'id',
					queryMode: 'local',
					store: Ext.create('Ext.data.Store',{
						fields: ['id','name'],
						proxy: {
							type: 'ajax',
							url: 'vm/getBzRole.do'
						}
					}),
					listeners:{
						afterrender:function(c){
							c.store.load({params:{
								bzId: rec.raw.bzid
							}});
							/*var bzname = c.up('form').down('combobox[name=bzname]');
							c.addManagedListener(bzname,'select',function(){
								c.setValue();
								c.store.load({params:{
									bzId: bzname.getSubmitValue()
								}});
							});*/
						}
					}
				},{
					xtype:'combobox',
					name:'gzjd',
					fieldLabel: '作业过程',
					readOlay:true,
					displayField: 'name',
					valueField: 'id',
					queryMode: 'local',
					store: Ext.create('Ext.data.Store',{
						//autoLoad: true,
						fields: ['id','name'],
						proxy: {
							type: 'ajax',
							url: 'vm/getGc.do'
						}
					}),
					listeners:{
						afterrender:function(c){
							c.store.load({params:{
								rwid: rec.raw.id
							}});
							/*var _bzname = c.up('form').down('combobox[name=bzname]');
							c.addManagedListener(_bzname,'select',function(){
								c.setValue();
								c.store.load({params:{
									bzid: _bzname.getSubmitValue()
								}});
							});*/
						}
					}
				}]
			},{
				region: 'center',
				xtype:'grid',
//				selModel: Ext.create('Ext.selection.CheckboxModel',{
//					mode:'SINGLE'
//				}),
				tbar:[{
					labelWidth:65,
					fieldLabel: '视频名称',
					xtype:'textfield'
				},{
					fieldLabel:'同步时间',
					xtype: 'datefield',
					labelWidth: 65,
					width: 170,
					format:'Y-m-d',
//					value:new Date(),
					submitFormat: 'Y-m-d'
				},{
					fieldLabel:'至',
					xtype: 'datefield',
					labelWidth: 20,
					width: 125,
//					value: new Date(),
					format:'Y-m-d',
					submitFormat: 'Y-m-d'
				},{
					text:'查询',
					width: 55,
					iconCls: 'icon-search',
					handler:function(){
						var xgrid = this.up('window').down('grid');
						xgrid.store.load({params:{
							videoName: xgrid.down('field[fieldLabel=视频名称]').getSubmitValue(),
							synStartTime: xgrid.down('field[fieldLabel=同步时间]').getSubmitValue(),
							synEndTime: xgrid.down('field[fieldLabel=至]').getSubmitValue()
						}});
					}
				},{
					text:'播放',
					width: 55,
					iconCls: 'icon-play',
					handler:function(){
						var rec = this.up('grid').getSelectionModel().getSelection()[0];
						if(rec){
							me.playWin(rec.raw);
						}
						else{
							Ext.Msg.alert("提示","请选择要播放的视频。");
						}
					}
				}],
				columns:[{
					text:'视频名称',dataIndex:'name',width:200
				},{
					text:'原始路径',dataIndex:'rawpath',width:200
				},{
					text:'同步时间',dataIndex:'syntime',width:200,renderer:formatDate
				},{
					text:'关联状态',dataIndex:'match',width:130,
					renderer: function(v){
						return v == 0?'<font color="blue">未关联</font>':'已关联';
					}
				}], 
	        	store:Ext.create('Ext.data.Store',{
					autoLoad: true,
					fields:['name','rawpath','syntime','match'],
					proxy:{
						type:'ajax',
						extraParams:{taskType:taskType},
						url:'vm/getSynVideo.do'
					}
	        	})
			}]
		}).show();
	},
	playWin:function(data){
		var me = this;
		Ext.create("Ext.window.Window",{
			layout:'fit',
			width:1000,
			height:600,
			modal:true,
			iconCls:'icon-play',
			title:'视频播放',
			items: [{
				xtype: 'player',
				autoPlay: true,
//				vdata:{id:rec.raw.video_},
//				src: basePath+rec.raw.synpath,
				vdata: data,
				src: data.synpath,
				border: false
			}]
		}).show();
	},
	upwin:function(rec,taskType,vmrec){
		var me = this;
		var upwindow = Ext.create("Ext.window.Window",{
			layout:'border',
			width:790,
			height:500,
			iconCls:'icon-cog_edit',
			modal:true,
			title:'视频关联',
			buttons:[{
				text:'保存',
				handler:function(){
					var thiswin = this.up("window");
					var taskId = rec.raw.id;
					//班组ID
					//var bzId = thiswin.down('combobox[name=bzname]').getValue();
					//班组角色ID
					var roleId = thiswin.down('combobox[name=bzjs]').getValue();
					//作业阶段ID
					var stageId = thiswin.down('combobox[name=gzjd]').getValue();
					//同步视频ID
					var videoId = thiswin.down('grid').getSelectionModel().getSelection()[0];
//					if(bzId == undefined){
//						Ext.Msg.alert('提示',"请选择工作班组");return;
//					}
					if(roleId == undefined){
						Ext.Msg.alert('提示',"请选择班组角色");return;
					}
					if(stageId == undefined){
						Ext.Msg.alert('提示',"请选工作阶段");return;
					}
					if(videoId == undefined){
						Ext.Msg.alert('提示',"请选择要关联的视频");return;
					}
					var delId = Ext.getCmp(me.id+'vgrid').getSelectionModel().getSelection()[0].raw.video_id;
					data = {
//						bz_id:bzId,
						bz_id:rec.raw.bzid,
						video_id:videoId.raw.id,
						stage_id:stageId,
						task_id:taskId,
						role_id:roleId,
						mark:delId || videoId.raw.id?'update':'',//判断是否为修改数据标识
						delId:Ext.getCmp(me.id+'vgrid').getSelectionModel().getSelection()[0].raw.video_id //已经关联的视频id,传入后天删除相关的数据
					};
					
					Ext.Ajax.request({
						url:'vm/vmcounts.do',
						params:{
							video_id:videoId.raw.id,
							task_id:taskId
						},
						success:function(resp){
							if(resp.responseText > 0){
								Ext.Msg.confirm('提示', '此视频已关联，点击"确定"将覆盖原数据？',function(x){
									if(x == 'yes'){
										me.vmUpAjax(data, upwindow);
									}
								});
							}else{
								me.vmUpAjax(data, upwindow);
							}
						}
					});
					
				
				}
			},{
				text:'关闭',
				handler:function(){
					upwindow.close();
				}
			}],
			items:[{
				region: 'north',
				xtype:'form',
				bodyPadding: 10,
				border: false,
				defaults: {anchor: '100%',allowBlank: false},
				items:[{
					xtype:'combobox',
					name:'bzjs',
					fieldLabel: '班组角色',
					readOlay:true,
					displayField: 'name',
					valueField: 'id',
					queryMode: 'local',
					store: Ext.create('Ext.data.Store',{
						fields: ['id','name'],
						proxy: {
							type: 'ajax',
							url: 'vm/getBzRole.do'
						}
					}),
					listeners:{
						afterrender:function(c){ 
							c.store.load({params:{
								bzId: rec.raw.bzid
							}});
							c.setValue(vmrec.raw.jsid);
							/*var bzname = c.up('form').down('combobox[name=bzname]');
							c.addManagedListener(bzname,'select',function(){
								c.setValue();
								c.store.load({params:{
									bzId: bzname.getSubmitValue()
								}});
							});*/
						}
					}
				},{
					xtype:'combobox',
					name:'gzjd',
					fieldLabel: '作业过程',
					readOlay:true,
					displayField: 'name',
					valueField: 'id',
					queryMode: 'local',
					store: Ext.create('Ext.data.Store',{
						//autoLoad: true,
						fields: ['id','name'],
						proxy: {
							type: 'ajax',
							extraParams:{rwid: rec.raw.id},
							url: 'vm/getGc.do'
						}
					}),
					listeners:{
						afterrender:function(c){
							c.store.load();
							c.setValue(vmrec.raw.gcid);
							/*var _bzname = c.up('form').down('combobox[name=bzname]');
							c.addManagedListener(_bzname,'select',function(){
								c.setValue();
								c.store.load({params:{
									bzid: _bzname.getSubmitValue()
								}});
							});*/
						}
					}
				}]
			},{
				region: 'center',
				xtype:'grid',
				/*selModel: Ext.create('Ext.selection.CheckboxModel',{
					mode:'SINGLE'
				}),*/
				tbar:[{
					labelWidth:65,
					fieldLabel: '视频名称',
					xtype:'textfield'
				},{
					fieldLabel:'同步时间',
					xtype: 'datefield',
					labelWidth: 65,
					width: 170,
					format:'Y-m-d',
//					value:new Date(),
					submitFormat: 'Y-m-d'
				},{
					fieldLabel:'至',
					xtype: 'datefield',
					labelWidth: 20,
					width: 125,
//					value: new Date(),
					format:'Y-m-d',
					submitFormat: 'Y-m-d'
				},{
					text:'查询',
					width: 55,
					iconCls: 'icon-search',
					handler:function(){
						var xgrid = this.up('window').down('grid');
						xgrid.store.load({params:{
							videoName: xgrid.down('field[fieldLabel=视频名称]').getSubmitValue(),
							synStartTime: xgrid.down('field[fieldLabel=同步时间]').getSubmitValue(),
							synEndTime: xgrid.down('field[fieldLabel=至]').getSubmitValue()
						}});
					}
				}],
				columns:[{
					text:'视频名称',dataIndex:'name',width:200
				},{
					text:'原始路径',dataIndex:'rawpath',width:200
				},{
					text:'同步时间',dataIndex:'syntime',width:200,renderer:formatDate
				},{
					text:'关联状态',dataIndex:'match',width:130,
					renderer: function(v){
						return v == 0?'<font color="blue">未关联</font>':'已关联';
					}
				}], 
	        	store:Ext.create('Ext.data.Store',{
					autoLoad: true,
					fields:['name','rawpath','syntime','match'],
					proxy:{
						type:'ajax',
						extraParams:{taskType:taskType},
						url:'vm/getSynVideo.do'
					}
	        	})
			}]
		}).show();
	},	
	delwin:function(rec,grid){
		var me = this;
		if(!rec.raw.video_id){
			Ext.Msg.alert('提示', '此作业过程无视频，删除无效！');
			return;
		}
		
		Ext.Msg.confirm('提示', '你确定要删除当前数据吗？',function(b){
			if('yes' == b){
				Ext.Ajax.request({
				    url: 'vm/deleteVSyn.do',
				    params: {
				        id: rec.raw.video_id,
				        name:rec.raw.name
				    },
				    success: function(response,action){
				    	if(response.responseText == 1){
				    		Ext.Msg.alert('提示', '删除成功！');
				    		grid.store.reload();
				    	}else{
				    		Ext.Msg.alert('提示', '删除失败！');
				    	}
				    }
				});
			}
		});
	},
	vmUpAjax:function(data,_window){
		var me = this;
		Ext.Ajax.request({
			url:'vm/updateVm.do',
			params:data,
			success:function(resp){
				//var d =Ext.decode(resp.responseText);
				if(resp.responseText == 1){
					Ext.getCmp(me.id+'vgrid').store.reload();
					_window.close();
					Ext.Msg.alert('提示', '关联成功！');
				}else{
					Ext.Msg.alert('提示', '关联失败！');
				}
			}
		});
	}
});